//
//  LYWebViewController.swift
//  WeChat
//
//  Created by mac on 2018/9/19.
//  Copyright © 2018年 RRC. All rights reserved.
//

import Foundation
import UIKit
import WebKit

private let kKVOContentSizeKey: String = "contentSize"
private let kKVOTitleKey: String = "title"

class LYWebViewController: UIViewController {
    var webView: WKWebView?
    var URLString: String!
    var titleString: String?
    
    init(title: String? = nil, URLString: String) {
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.init(ts_hexString: "#2D3132")
        
        let prefernces = WKPreferences()
        prefernces.javaScriptEnabled = false
        
        let configuration = WKWebViewConfiguration()
        configuration.preferences = prefernces
        
        self.webView = WKWebView(frame: self.view.bounds, configuration: configuration)
        guard let theWebView = self.webView else {
            return
        }
        
        theWebView.scrollView.bounces = true
        theWebView.scrollView.isScrollEnabled = true
        theWebView.navigationDelegate = self
        
        let urlRequest = URLRequest(url: URL(string: URLString!)!)
        theWebView.load(urlRequest)
        self.view.addSubview(theWebView)
        
        theWebView.addObserver(self, forKeyPath: kKVOContentSizeKey, options: .new, context: nil)
        theWebView.addObserver(self, forKeyPath: kKVOTitleKey, options: .new, context: nil)
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        switch keyPath {
        case kKVOContentSizeKey?:
            if let height = change![NSKeyValueChangeKey.newKey] as? Float {
                self.webView?.scrollView.contentSize.height = CGFloat(height)
            }
            break
            
        case kKVOTitleKey?:
            if self.titleString != nil {
                return
            }
            if let title = change![NSKeyValueChangeKey.newKey] as? String {
                self.title = title
            }
            break
            
        default:
            break
        }
    }
    
    deinit {
        self.webView?.removeObserver(self, forKeyPath: kKVOContentSizeKey)
        self.webView?.removeObserver(self, forKeyPath: kKVOTitleKey)
    }
    
}

// MARK: - WKNavigationDelegate
extension LYWebViewController: WKNavigationDelegate{
    //页面开始加载
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }
    
    //页面加载完成
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }
    
    ///服务器开始请求的时候调用
    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        decisionHandler(.allow)
    }
    
}
